<!-- 选择星期的一个组件，该组件只需要修改日期 -->
<template>
  <div class="week-select">
    <div class="week-select-header">
      <slot name="header-left"></slot>
      <div class="week-select-container">
        <div class="week-select-button" @click="changeDate('-1')">
          <el-icon><ArrowLeft /></el-icon>
          <span>上周</span>
        </div>
        <div class="relative">
          <el-date-picker
            class="date-picker"
            v-model="date"
            type="date"
            value-format="YYYY-MM-DD"
            @change="changeDate"
          />
          <div class="week-str">
            <span>{{ startStr }}</span>
            <span>~</span>
            <span>{{ endStr }}</span>
          </div>
        </div>
        <div class="week-select-button" @click="changeDate('1')">
          <span>下周</span>
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>
      <slot name="header-right"></slot>
    </div>
    <slot></slot>
    <div class="date_list">
      <div
        v-for="(item, index) in weekList"
        :key="index"
        class="date_item"
        :class="{ active: item.date == date }"
        @click="dateClickHandle(item)"
      >
        {{ item.date.slice(5) }} 星期{{ item.week }}
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getNextWeekDate, getPreviousWeekDate, getWeekDates, WeekDate } from '@/utils/moment'

const props = defineProps({
  modelValue: {
    type: String,
    default: '',
  },
})
const emit = defineEmits(['update:modelValue'])

const weekList = ref<WeekDate[]>([])

const date = ref('')
const startStr = ref('')
const endStr = ref('')
date.value = props.modelValue
watch(date, newVal => {
  emit('update:modelValue', newVal)
})
const changeDate = (val: string) => {
  let dates = val
  if (val === '1') {
    dates = getNextWeekDate(date.value)
    date.value = dates
  } else if (val === '-1') {
    dates = getPreviousWeekDate(date.value)
    date.value = dates
  } else {
    dates = val
  }
  weekList.value = getWeekDates(dates)
  startStr.value = weekList.value[0].date
  endStr.value = weekList.value[6].date
}
changeDate(date.value)

const dateClickHandle = (item: WeekDate) => {
  date.value = item.date
}
</script>

<style scoped lang="scss">
.week-select {
  .week-select-header {
    position: relative;
    min-height: 32px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .week-select-container {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 20px;
    top: 0;
    .week-select-button {
      padding: 6px 16px;
      border-radius: 4px;
      border: 1px solid #ebebeb;
      display: flex;
      align-items: center;
      gap: 5px;
      cursor: pointer;
      &:hover {
        color: $main-color;
        border-color: $main-color;
      }
    }
    .week-str {
      display: flex;
      align-items: center;
      gap: 10px;
      span {
        font-size: 22px;
        font-weight: bold;
      }
    }
    ::v-deep .el-date-editor {
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      opacity: 0;
      width: 100%;
      cursor: pointer;
      .el-input__wrapper {
        cursor: pointer;
        .el-input__inner {
          cursor: pointer;
        }
      }
    }
  }
  .date_list {
    height: 43px;
    background: #f6f7f9;
    display: flex;
    .date_item {
      flex: 1;
      text-align: center;
      line-height: 44px;
      color: #2c2c2c;
      font-size: 14px;
      font-weight: 700;
      height: 44px;
      cursor: pointer;
      &.active {
        color: #239bf4;
      }
    }
  }
}
</style>
